package com.yindian.cos.monitor.interceptor;

import com.yindian.cos.monitor.core.TrackerService;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;

/**
 * 如果使用restTemplate请注入此拦截器，会自动在heard中封装tracker_id
 *
 * @author zhangchuanhao
 * @create 2018/11/5 11:39
 */
public class TrackerRestTemplateInterceptor implements ClientHttpRequestInterceptor {
    @Override
    public ClientHttpResponse intercept(HttpRequest httpRequest, byte[] bytes, ClientHttpRequestExecution execution) throws IOException {
        // 将当前trackerId放入请求头
        httpRequest.getHeaders().add(TrackerService.TRACKER_ID, TrackerService.getTrackerId());
        return execution.execute(httpRequest, bytes);
    }
}
